package com.gin.no_gay_alliance.customview;

import android.content.Context;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.widget.FrameLayout;

import androidx.fragment.app.FragmentActivity;

import com.gin.nga.bbscode.entity.BbsTag;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter.ViewHolder;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.utils.ImageUtils;
import com.gin.no_gay_alliance.utils.MyHandler;

import java.util.List;

/**
 * 正文渲染器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/1 11:43
 **/
public class ContentWebViewRender {
    public static final String TAG = ContentWebViewRender.class.getSimpleName();
    private final Context context;
    private final MyHandler handler;
    /**
     * 池子
     */
    private final ContentWebView[] pool = new ContentWebView[40];
    private final WebChromeClient webChromeClient = new WebChromeClient();
    private final ContentWebViewClient contentWebViewClient;

    public ContentWebViewRender(Context context, MyHandler handler) {
        this.context = context;
        contentWebViewClient = new ContentWebViewClient(context);
        this.handler = handler;
    }

    /**
     * 预览图片，暴露给WebView
     *
     * @param url url
     */
    @JavascriptInterface
    public void onClickImg(String url) {
        // 弹窗选择执行的操作
        if (context instanceof FragmentActivity) {
            // 删除缩略图后缀，预览原图
            handler.post(() -> ImageUtils.preview(((FragmentActivity) context), ImageUtils.removeSuffix(url)));
        } else {
            handler.toast("无法预览");
        }
    }

    public void render(int position, ViewGroup layoutContent, ViewHolder<?> holder, List<BbsTag> nodes) {
        // 从数组的该位置获取 wv
        ContentWebView wv = pool[position];
        // 如果没找到, 新建一个放过去
        if (wv == null) {
            wv = new ContentWebView(MyApplication.app);
            wv.setWebChromeClient(webChromeClient);
            wv.setWebViewClient(contentWebViewClient);
            wv.addJavascriptInterface(this, "android");
            wv.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            pool[position] = wv;
        }
        // 之前保存的wv
        String key = ContentWebView.class.getSimpleName();
        ContentWebView tag = (ContentWebView) holder.objectMap.get(key);
        if (wv != tag) {
            // 如果拿到的wv和holder中保存的不一致, 移除holder中的wv
            if (tag != null) {
                layoutContent.removeView(tag);
                holder.objectMap.put(key, null);
            }
            // 如果拿到的wv有父级，从其中出来
            if (wv.getParent() != null) {
                ((ViewGroup) wv.getParent()).removeView(wv);
            }
            // 把拿到的wv放入布局
            holder.objectMap.put(key, wv);
            layoutContent.addView(wv);
        }
        wv.loadNodes(nodes);
    }

    /**
     * 是否阻止WebView加载图片
     *
     * @param blocked 是否阻止加载图片
     */
    public void setBlockNetworkImage(boolean blocked) {
        for (ContentWebView wv : pool) {
            if (wv != null) {
                wv.getSettings().setBlockNetworkImage(blocked);
            }
        }
    }

}
